﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLKS
{
    public partial class f_main : DevExpress.XtraBars.Ribbon.RibbonForm
    {

        public Mysql qmysql = new Mysql();
        public f_Login flogin = new f_Login();
        public Dictionary<string, string> GetPassFromUser;
        public f_main()
        {
            InitializeComponent();
            barStaticItem3.Caption = DateTime.Now.ToLongTimeString();
           
        }
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            QLP nform = new QLP();
            this.CheckCloseForm("QLP", nform);
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DMP nform = new DMP();
            this.CheckCloseForm("DMP", nform);
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            QLNV nform = new QLNV();
            this.CheckCloseForm("QLNV", nform);
            
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DSNV nform = new DSNV();
            this.CheckCloseForm("DSNV", nform);
            
        }
        private void barButtonItem10_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            QLLP nform = new QLLP();
            this.CheckCloseForm("QLLP", nform);
        }

        private void barButtonItem12_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            QLCV nform = new QLCV();
            this.CheckCloseForm("QLCV", nform);
        }
        private void barButtonItem13_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            PNP nform = new PNP();
            this.CheckCloseForm("PNP", nform);
        }

        public void CheckCloseForm(string OpenForm, Form NameForm)
        {
            Form[] ChildArray = this.MdiChildren;
            foreach (Form childform in ChildArray)
            {
                childform.Close();
            }
            NameForm.MdiParent = this;
            NameForm.Show();
                
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            About gioithieu = new About();
            gioithieu.Show();
         
        }

        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void f_main_Load(object sender, EventArgs e)
        {
            this.flogin.ShowDialog();
            if(flogin.Logined == 1)
            {
                DMP dmp = new DMP();
                dmp.MdiParent = this;
                dmp.Show();
            }
            
        }

        private void f_main_Activated(object sender, EventArgs e)
        {
            if (this.flogin.Logined == 1)
            {
                this.Enabled = true;
                
               
                //active button with chuc vu
                if (this.flogin.GetPassFromUser["MaCV"] == "2")
                {
                    this.bar_welcome.Caption = "Chào mừng nhân viên:  " + this.flogin.GetPassFromUser["TenNV"] + " quay trở lại với phiên làm việc.";
                    this.bt_pnp.Enabled = true;
                    this.bt_dmp.Enabled = true;
                }

                if (this.flogin.GetPassFromUser["MaCV"] == "1")
                {
                    this.bar_welcome.Caption = "Chào mừng quản lý: " + this.flogin.GetPassFromUser["TenNV"] + " quay trở lại với phiên làm việc.";
                    this.bt_pnp.Enabled = true;
                    this.bt_dmp.Enabled = true;
                    this.bt_qlp.Enabled = true;
                    this.bt_qlnv.Enabled = true;
                    this.bt_cd.Enabled = true;
                    this.bt_lp.Enabled = true;
                    this.bt_cv.Enabled = true;

                }
               
                
            }
        }

        private void bt_plv_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.flogin.Logined == 1)
            {
                DialogResult plv = MessageBox.Show("Bạn có chắc chắn muốn đổi phiên làm việc cho một tài khoản khác không ?", "Đổi Phiên Làm Việc", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (plv.ToString() == "Yes")
                {
                    string cmdText = "UPDATE ht_nhanvien SET Trangthai=?MaTT WHERE MaNV=?manv";
                    Dictionary<string, string> Params_stt = new Dictionary<string, string>();
                    Params_stt["MaTT"] = "0";
                    Params_stt["manv"] = this.flogin.GetPassFromUser["MaNV"];
                    this.qmysql.NonQueryMysqlCommand(cmdText, Params_stt);
                    this.flogin.Logined = 0;
                    this.flogin.ShowDialog();
                }
            }
            else
            {
                this.flogin.ShowDialog();
            }
        }

        private void f_main_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult thoat = MessageBox.Show("Bạn có chắc muốn thoát?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (thoat.ToString() == "Yes")
            {
                if (this.flogin.Logined == 1)
                {
                    string cmdText = "UPDATE ht_nhanvien SET Trangthai=?MaTT WHERE MaNV=?manv";
                    Dictionary<string, string> Params_stt = new Dictionary<string, string>();
                    Params_stt["MaTT"] = "0";
                    Params_stt["manv"] = this.flogin.GetPassFromUser["MaNV"];
                    this.qmysql.NonQueryMysqlCommand(cmdText, Params_stt);
                }
            }
            else {
                e.Cancel=true;
            }
        }

        private void bt_cd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frm_Setting caidat = new frm_Setting();
            caidat.TopMost = true;
            caidat.Show();
        }

        
    }
}
